#include <iostream>
using namespace std;

const int N = 310;
const int INF = 0x3f3f3f3f;

int n;
int a;      //存储石子
int sum[N]; //前缀和
int f[N][N];

int main()
{
    freopen("cin.txt", "r", stdin);
    cin >> n;
    for (int i = 1; i <= n; ++i)
    {
        cin >> a;
        sum[i] = sum[i - 1] + a;
    }
    for (int i = 1; i < n; ++i)          //要合并的两堆之间的间隔（l-r）
        for (int j = 1; j + i <= n; ++j) //合并区间的起点l
        {
            f[j][j + i] = INF;
            for (int k = 0; k < i; ++k) //分割点（属于前面）
                f[j][j + i] = min(f[j][j + i], f[j][j + k] + f[j + k + 1][j + i] + sum[j + i] - sum[j - 1]);
        }
    cout << f[1][n];
    return 0;
}